var url =require("url");
var express = require("express");
var app = express();
var mysql = require("mysql");
var ejs = require("ejs");

app.use(express.static(__dirname+"/static"));//设置静态目录static

//创建数据库
sql = mysql.createConnection({
	host:'localhost',
	user:'root',
	password:"195000",
	database:'hyj'
})

sql.connect();

//用户首页请求登录
app.get("/new",(req,res)=>{
	sql.query("select id,password from yonghu",(err,data)=>{
		if(!err){
			console.log(data)
			res.end(JSON.stringify(data))
		}
		else{
			console.log(err)
			res.end("err")
		}
	})
})
app.get("/news",(req,res)=>{
	var id = url.parse(req.url,true).query.id;
	sql.query(`SELECT * FROM yonghu WHERE id="${id}"`,(err,data)=>{
		if(!err){
			ejs.renderFile("static/todolist.html",{data:data[0]},(err,str)=>{
				if(!err){
					res.end(str);
					console.log("ok")
				}
				else{
					res.end("err");
					console.log(err);
				}
			})
		}
		else{
			res.end("err")
			console.log(err)
		}
	})
})



app.listen(100);
console.log('server is running at 100')